package sk.fiit.vppj.eventmanager.gui.administrator;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.List;

/**
 * Class responsible for creating and printing ID Card
 * 
 * @author Marek
 * 
 */
final class IdCardPrinter implements Printable {

	private List<IdCard> cards = null;

	// private final static Logger log = Logger.getLogger( IdCardPrinter.class.getName() );

	public IdCardPrinter(List<IdCard> users) {
		this.cards = users;
	}

	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
		if( pageIndex > (this.cards.size()-1)/4 ) {
			return NO_SUCH_PAGE;
		}
		Graphics2D g2d = ( Graphics2D ) graphics;
		g2d.translate( 0, 20 );
		g2d.translate( 0, -pageIndex*850 );

		int width = 20;
		int widthSize = 280;
		int tempWidth = 0;

		int height = 20;
		int heightSize = 380;
		int heightPageBreak = 90;
		int tempHeight = 0;

		for ( int i = 0; i < cards.size(); i++ ) {
			
			if( ( i % 2 ) == 0 ) {
				tempWidth = width;
			}
			else
			{
				tempWidth = width + widthSize;		
			}
			
			tempHeight = height + (i/2)*heightSize + (i/4)*heightPageBreak;
			
			graphics.drawImage( cards.get( i ).getCard(), tempWidth, tempHeight, 290, 387, null );
		}
		return PAGE_EXISTS;
	}

}
